package com.bening.smsapp.filechangereg;

import org.apache.log4j.Logger;

import java.util.List;
import com.bening.smsapp.util.*;
import com.bening.smsapp.exception.*;
import com.bening.smsapp.bean.MachineSource;
import com.bening.smsapp.bean.PathSource;
import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.SmsAppConstant;
import com.bening.smsapp.downloader.FtpClient;

public class FileChangeProcess
{
    /**
     * real process in the remote host file change registration.
     */
    public static void process( BootConfigParameters params )
    {
        final Logger logger = Logger.getLogger( FileChangeProcess.class );
        final String METHOD_SIGNATURE = "[filechangereg]";
        System.out.println("[File Register]");
        DataFetchProcess process     = DataFetchProcess.getInstance();
        FtpClient ftpClient = new FtpClient();
        List machine = params.getMachineSourceList();

        for(int i=0;i<machine.size();i++) {
        	MachineSource m = (MachineSource)machine.get(i);
	        String host     = m.getIpAddress();
	        int port        = m.getPort();
	        String username = m.getUsername();
	        String password = m.getPassword();
	        int pathSource = 0;
	        if(m.getMachineType()==SmsAppConstant.FLAG_REMOTE_MACHINE) {
		        try
		        {
		        	logger.info( METHOD_SIGNATURE + "logging in...." );
		            ftpClient.login( host, port, username, password );
		            List sourceList = m.getSourcePathList();
		            if(sourceList.size()!=0) {
			        	for(int j=0;j<sourceList.size();j++)  {
			        		// for each node
			        		PathSource p = (PathSource) sourceList.get(j);
			        		pathSource = p.getPathSourceId();
			        		List remoteFileList = ftpClient.getFileList(p,params );
			                List registeredFileList = process.getRegisteredFilesList(m,p);
			                // comparing file list in database with file list in remote directory
			                List differenceList = SmsAppTools.getListDifference( registeredFileList, remoteFileList );
			                
			                if( differenceList != null )
			                {
			                    if ( logger.isInfoEnabled() )
			                    {
			                        logger.info( METHOD_SIGNATURE + " found " + differenceList.size() + " differences." );
			                    }
			                    process.storeToBeDownloadedList( differenceList, SmsAppConstant.FLAG_REMOTE_MACHINE, pathSource );
		
			                }
			                else
			                {
			                    if ( logger.isInfoEnabled() )
			                    {
			                        logger.info( METHOD_SIGNATURE + "no new data found in node "+p.getNodeId() );
			                    }
			                }
			                
			        	}
		        }
		            ftpClient.logout();
		        }
		        catch ( SmsAppException e )
		        {
		            logger.error( e );
		            SmsAppTools.InsertEventLog(SmsAppConstant.REGISTER_ERROR, e.getMessage(),pathSource);
		            
		        }
	        }else if(m.getMachineType()==SmsAppConstant.FLAG_LOCAL_MACHINE) {

	            List sourceList = m.getSourcePathList();
	            if(sourceList.size()!=0) {
		        	for(int j=0;j<sourceList.size();j++)  {
		        		// for each node
		        		PathSource p = (PathSource) sourceList.get(j);
		        		pathSource = p.getPathSourceId();
		        		List localFileList = SmsAppTools.getLocalFileList(p );
		                List registeredFileList = process.getRegisteredFilesList(m,p);
		                // comparing file list in database with file list in remote directory
		                List differenceList = SmsAppTools.getListDifference( registeredFileList, localFileList );
		                
		                if( differenceList != null )
		                {
		                    if ( logger.isInfoEnabled() )
		                    {
		                        logger.info( METHOD_SIGNATURE + " found " + differenceList.size() + " differences." );
		                    }
		                    process.storeToBeDownloadedList( differenceList, SmsAppConstant.FLAG_LOCAL_MACHINE, pathSource );
	
		                }		                
		        	}
	        }

	        	
	        	
	        } else {
	        	
	        }
	
	      }

    }
}


